import 'package:get/get.dart';
import '../controllers/device_data_controller.dart';

class DeviceDataBinding extends Bindings {
  @override
  void dependencies() {
    // 获取传递的参数
    final args = Get.arguments as Map<String, dynamic>?;
    final deviceId = args?['deviceId'] as int? ?? 0;
    final deviceName = args?['deviceName'] as String? ?? '';
    
    print('DeviceDataBinding: deviceId=$deviceId, deviceName=$deviceName');
    
    try {
      // 尝试查找已存在的控制器，如果存在则复用
      if (Get.isRegistered<DeviceDataController>(tag: 'device_data_$deviceId')) {
        print('控制器已存在，复用现有实例: device_data_$deviceId');
        Get.find<DeviceDataController>(tag: 'device_data_$deviceId');
      } else {
        // 如果不存在则创建新控制器
        print('创建新控制器实例: device_data_$deviceId');
        Get.put<DeviceDataController>(
          DeviceDataController(
            deviceId: deviceId,
            deviceName: deviceName,
          ),
          permanent: true,
          tag: 'device_data_$deviceId',
        );
      }
    } catch (e) {
      print('控制器注册异常: $e');
      // 确保总是有一个可用的控制器实例
      Get.put<DeviceDataController>(
        DeviceDataController(
          deviceId: deviceId,
          deviceName: deviceName,
        ),
        permanent: true,
        tag: 'device_data_$deviceId',
      );
    }
  }
} 